package com.smart.thrift.model.common;

import com.smart.thrift.constants.ThriftConstants;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Getter
@Setter
@ConfigurationProperties(prefix = "spring.thrift.client")
public class ThriftClientProperties {

    /**
     * 服务模型(单线程/多线程/阻塞/非阻塞)
     * <p>
     * simple: 单线程阻塞模型
     * nonBlocking: 单线程非阻塞模型
     * threadPool: 线程池同步模型
     * hsHa: 半同步半异步模型
     * threadedSelector: 线程池选择器模型
     * </p>
     */
    private String serviceModel = ThriftConstants.ServiceModel.SERVICE_MODEL_DEFAULT;


    private ThriftClientDiscoveryProperties discovery = new ThriftClientDiscoveryProperties();

    private ThriftClientPoolProperties pool = new ThriftClientPoolProperties();


    @Getter
    @Setter
    public static class ThriftClientDiscoveryProperties {

        /**
         * 服务注册中心的类型默认zookeeper
         */
        private String type = ThriftConstants.DiscoveryType.DISCOVERY_DEFAULT;

        /**
         * 服务注册中心的地址
         */
        private String host = "127.0.0.1";

        /**
         * 服务注册中心的端口号(默认zookeeper：2181)
         */
        private Integer port = 2181;

        /**
         * 从注册中心获取服务的方式目前只提供循环
         */
        private String balanceRule = "ROBIN";

        /**
         * 服务健康检查,consul定时查询节点状态，zookeeper可以注册监听器。用于客户端调用时选择可用的服务
         */
        private ThriftServerOnlineCheckProperties onlineCheck;


        /**
         * 客户端服务检查，仅代理服务可用，客户端会间隔一定时间ping注册的远程服务，
         * zookeeper注册中心有watcher机制，无需使用
         */
        @Getter
        @Setter
        public static class ThriftServerOnlineCheckProperties {

            /**
             * 是否允许健康检查
             */
            private Boolean enabled = false;

            /**
             * 服务健康检查时间间隔 (默认30s)
             */
            private Long checkInterval = 30L;

            /**
             * 服务健康检查超时时间（默认10s）
             */
            private Long checkTimeout = 10L;

        }

    }

    @Getter
    @Setter
    public static class ThriftClientPoolProperties {

        private int retryTimes = 3;

        private int connectTimeout = 10000;

        private int poolMaxTotalPerKey = 60;

        private int poolMaxIdlePerKey = 40;

        private int poolMinIdlePerKey = 3;

        private long poolMaxWait = 180000;

        /**
         * 池对象创建时时验证是否正常可用
         */
        private boolean testOnCreate = true;

        /**
         * 池对象借出时验证是否正常可用
         */
        private boolean testOnBorrow = true;


        /**
         * 池对象归还时验证是否正常可用
         */
        private boolean testOnReturn = true;

        /**
         * 空闲连接自动被空闲连接回收器
         */
        private boolean isTestWhileIdle = true;

    }

}
